-- UIBuyGem
-- Created by chengb Feb/5/2015
-- 购买钻石界面

UIBuyGem = class("UIBuyGem", function()
    return cc.Layer:create();    
end);

function UIBuyGem.create()
    return UIBuyGem.new();
end

-- 钻石购买配置
-- TODO 暂时写死
local GEM_BUY_CONFIG;

-- 内部函数声明
local resize;

-- 构造函数
function UIBuyGem:ctor()
    self:setName("UIBuyGem");

    GEM_BUY_CONFIG = {
        {["gem_amount"] = 100,  ["desc"] = getLocStr("gem_buy_desc1"), ["price"] = 6,},
        {["gem_amount"] = 300, ["desc"] = getLocStr("gem_buy_desc2"), ["price"] = 18,},
        {["gem_amount"] = 1000, ["desc"] = getLocStr("gem_buy_desc3"), ["price"] = 60,},
        {["gem_amount"] = 5500, ["desc"] = getLocStr("gem_buy_desc4"), ["price"] = 300,},
    };

    -- 初始化
    local node = cc.CSLoader:createNode("layout/market/BuyGem.csb");
    self:addChild(node);
    self.node = node;
    local scrollView = findChildByName(node, "CT/scollview");
    self.scrollView = scrollView;
    
    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);
    
    -- 注册点击事件
    self:registerTouchEvent();
    
    -- 重绘
    self:redraw();
    
    -- 适配
    resize(node);
end

-- 注册点击事件
function UIBuyGem:registerTouchEvent()
    local BT = findChildByName(self.node, "BT");

    -- 注册返回按钮点击事件
    local btnBack = findChildByName(BT, "btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then  
            -- 关闭当前界面
            AudioM.playFx("button_return");  
            UIMgr.getCurrentScene():removeFormByName("UIBuyGem");
        end
    end
    btnBack:addTouchEventListener(onBackClick);
end

-- 适配
function resize(node)
    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT"); 
end

-- 重绘
function UIBuyGem:redraw()
    -- 设置返回按钮的文本样式
    local btnBack = findChildByName(self.node, "BT/btn_back");
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));
    
    -- 重绘滚动容器
    self:redrawScrollView();
end

-- 重绘滚动容器
function UIBuyGem:redrawScrollView()
    -- 先清空
    local scrollView = self.scrollView;
    scrollView:removeAllChildren();

    -- local itemNode = cc.CSLoader:createNode("layout/market/GemBuyItem.csb");
    -- local widget = replaceNodeWithWidget(itemNode);

    local totalNum = #GEM_BUY_CONFIG;

    --左边距
    local leftMargin = 10;

    -- 行间距
    local gap = 0;
    local widgetHeight = 130;

    -- 计算总高度
    local totalHeight = math.max(totalNum * (widgetHeight + gap), scrollView:getInnerContainerSize().height);
    scrollView:setInnerContainerSize(cc.size(scrollView:getInnerContainerSize().width, totalHeight));     

    for i = 1, totalNum do
        local info = GEM_BUY_CONFIG[i];

        -- TODO 这里clone会挂掉，暂不知为何
        --local child = widget:clone();
        local child = cc.CSLoader:createNode("layout/market/MarketItem.csb");
        child = replaceNodeWithWidget(child);
        child:setAnchorPoint(cc.p(0, 0));
        child:setPositionX(leftMargin);
        child:setPositionY(totalHeight - i * (widgetHeight + gap));

        -- 显示钻石数量
        local btnBuy = findChildByName(child, "btn_buy");
        local gemAmountLabel = findChildByName(btnBuy, "gem_amount");
        gemAmountLabel:setString(tostring(info["gem_amount"]));

        -- 显示描述
        local descLabel = findChildByName(btnBuy, "desc");
        descLabel:setString(info["desc"]);

        -- 显示价格
        local priceLabel = findChildByName(btnBuy, "price");
        priceLabel:setString(tostring(info["price"]));

        -- 注册按钮点击事件
        local function onBuyClick(sender, eventType)
            if eventType == ccui.TouchEventType.ended then  
                AudioM.playFx("button_click");  
                alert(getLocStr("disable_function"));
            end
        end
        btnBuy:addTouchEventListener(onBuyClick);

        self.scrollView:addChild(child);
    end

    self.scrollView:scrollToTop(0.1, true);
end




